package com.witmore.neutron.ai.web.apiv2.stock;

import com.jk.common.response.ResponsePageResult;
import com.jk.common.response.ResponseResult;
import com.witmore.neutron.ai.web.wechat.controller.ScorpionBaseController;
import com.witmore.neutron.ai.web.wechat.dto.StockAlertConfigDTO;
import com.witmore.neutron.ai.web.wechat.dto.StockAlertConfigQueryDTO;
import com.witmore.neutron.ai.web.wechat.service.StockAlertConfigService;
import com.witmore.neutron.ai.web.wechat.utils.StockUtil;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;

import java.util.Map;


/**
 * V2 股票预警配置控制器
 * Author: 豆子高
 * Date: 2025/12/19
 */
@Slf4j
@RestController
@RequestMapping("/v2/stock-alert")
@Tag(name = "V2 股票预警配置管理", description = "股票异动预警配置的增删改查接口")
public class StockAlertConfigControllerV2 extends ScorpionBaseController {

    @Autowired
    private StockAlertConfigService stockAlertConfigService;

    @Autowired
    private StockUtil stockUtil;

    /**
     * 创建股票预警配置
     *
     * @param configDTO 配置信息
     * @return 创建结果
     */
    @PostMapping("/create")
    @Operation(summary = "创建股票预警配置", description = "为用户创建新的股票异动预警配置")
    public ResponseResult<String> createAlertConfig(@RequestBody StockAlertConfigDTO configDTO) {
        log.info("创建股票预警配置请求: {}", configDTO);

        super.validateUserLogin();

        // 校验stockCodes参数格式
        Map<String, String> stringStringMap = stockUtil.validateStockCodes(configDTO.getStockCodes());
        // 解析出无效的代码
        String msg = "创建成功";
        if (!CollectionUtils.isEmpty(stringStringMap) && stringStringMap.containsKey("invalidCodes")) {
            msg = stringStringMap.get("invalidCodes");
            stringStringMap.remove("invalidCodes");
        }
        if (CollectionUtils.isEmpty(stringStringMap)) {
            return ResponseResult.fail(400, msg);
        }
        configDTO.setStringStringMap(stringStringMap);
        try {
            return stockAlertConfigService.createAlertConfig(configDTO);

        } catch (Exception e) {
            log.error("创建股票预警配置异常", e);
            return ResponseResult.fail(500, "创建失败：" + e.getMessage());
        }
    }

    /**
     * 更新股票预警配置
     *
     * @param configDTO 配置信息
     * @return 更新结果
     */
    @PutMapping("/update")
    @Operation(summary = "更新股票预警配置", description = "更新现有的股票异动预警配置")
    public ResponseResult<String> updateAlertConfig(@RequestBody StockAlertConfigDTO configDTO) {
        log.info("更新股票预警配置请求: {}", configDTO);

        super.validateUserLogin();

        try {
            return stockAlertConfigService.updateAlertConfig(configDTO);

        } catch (Exception e) {
            log.error("更新股票预警配置异常", e);
            return ResponseResult.fail(500, "更新失败：" + e.getMessage());
        }
    }

    /**
     * 删除股票预警配置
     *
     * @param id 配置ID
     * @return 删除结果
     */
    @DeleteMapping("/delete/{id}")
    @Operation(summary = "删除股票预警配置", description = "删除指定的股票异动预警配置")
    public ResponseResult<String> deleteAlertConfig(@PathVariable Long id) {
        log.info("删除股票预警配置请求, ID: {}", id);

        super.validateUserLogin();
        try {
            return stockAlertConfigService.deleteAlertConfig(id);

        } catch (Exception e) {
            log.error("删除股票预警配置异常", e);
            return ResponseResult.fail(500, "删除失败：" + e.getMessage());
        }
    }

    /**
     * 根据ID获取股票预警配置
     *
     * @param id 配置ID
     * @return 配置信息
     */
    @GetMapping("/get/{id}")
    @Operation(summary = "获取股票预警配置", description = "根据ID获取指定的股票异动预警配置")
    public ResponseResult<StockAlertConfigDTO> getAlertConfig(@PathVariable Long id) {
        log.info("获取股票预警配置请求, ID: {}", id);
        super.validateUserLogin();

        try {
            return stockAlertConfigService.getAlertConfigById(id);

        } catch (Exception e) {
            log.error("获取股票预警配置异常", e);
            return ResponseResult.fail(500, "获取失败：" + e.getMessage());
        }
    }

    /**
     * 分页查询股票预警配置
     *
     * @param queryDTO 查询条件
     * @return 分页结果
     */
    @PostMapping("/query")
    @Operation(summary = "查询股票预警配置", description = "分页查询用户的股票异动预警配置列表")
    public ResponsePageResult<StockAlertConfigDTO> queryAlertConfigs(@RequestBody StockAlertConfigQueryDTO queryDTO) {
        log.info("查询股票预警配置请求: {}", queryDTO);
        super.validateUserLogin();
        try {
            return stockAlertConfigService.queryAlertConfigs(queryDTO);

        } catch (Exception e) {
            log.error("查询股票预警配置异常", e);
            return ResponsePageResult.fail(500, "查询失败：" + e.getMessage());
        }
    }

}
